package com.ruoyi.passenger.orderevaluate.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.driver.order.service.DriverOrderService;
import com.ruoyi.driver.order.vo.DriverOrderVo;
import com.ruoyi.driver.orderevaluate.domain.OrderEvaluateDriver;
import com.ruoyi.passenger.orderevaluate.mapper.OrderEvaluateMapper;
import com.ruoyi.passenger.orderevaluate.ro.*;
import com.ruoyi.passenger.orderevaluate.service.OrderEvaluateService;
import com.ruoyi.passenger.orderevaluate.vo.GetOrderEvaluateHandleVO;
import com.ruoyi.passenger.orderevaluate.vo.OrderEvaluateByOrderIdVO;
import com.ruoyi.passenger.orderevaluate.vo.OrderEvaluateByPassengerIdVO;
import com.ruoyi.passenger.user.service.PassengerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 乘客评价管理
 *
 * @author ruoyi
 * @date 2021-12-01
 */
@Service
public class OrderEvaluateServiceImpl implements OrderEvaluateService {
    @Resource
    private OrderEvaluateMapper orderEvaluateMapper;
    @Autowired
    private DriverOrderService driverOrderService;
    @Autowired
    private PassengerUserService passengerUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OrderEvaluateByPassengerIdVO> getEvaluatelistByPassengerid(OrderEvaluateListParam param) {
        List<OrderEvaluateByPassengerIdVO> list = orderEvaluateMapper.getEvaluatelistByPassengerid(param);
        for (OrderEvaluateByPassengerIdVO vo :list)
        {
            Date evaluateTime = vo.getEvaluateTime();
            Date date = new Date();
            if (null != vo.getFinishTime())
            {
                date = vo.getFinishTime();
            }
            if(null != evaluateTime){
                vo.setDatePoor(DateUtils.getDatePoor(date,evaluateTime));
            }
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderEvaluateByOrderIdVO getEvaluatelistByOrderid(OrderEvaluateByOrderIdParam param) {
        OrderEvaluateByOrderIdVO vo = new OrderEvaluateByOrderIdVO();
        if(param.getOrderId() == null && param.getOrderEvaluateId() == null){
            throw new ServiceException("投诉id或订单id不能为空");
        }
        if(param.getOrderId() != null && param.getOrderEvaluateId() != null){
            throw new ServiceException("投诉id和订单id只能传一个");
        }
        if(param.getOrderId() != null){
            vo = orderEvaluateMapper.getEvaluatelistByOrderid(param);
        }
        if(param.getOrderEvaluateId() != null){
            vo = orderEvaluateMapper.getEvaluatelistById(param);
        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrderevaluate(AddOrderEvaluateParam param,String userName,String code) {
        Long userId = null;
        DriverOrderVo order = null;
        if("xiaochengxu".equals(code)){
            if(null == param.getOrderId()){
                throw new ServiceException("订单id不能为空");
            }
            if(StringUtils.isEmpty(userName)){
                throw new ServiceException("获取当前用户失败，请稍后重试");
            }
            userId = passengerUserService.getUserId(userName);
            if(null == userId){
                throw new ServiceException("获取当前用户失败，请稍后重试");
            }
            order = driverOrderService.getDriverOrderById(param.getOrderId());
        }else {
            if(StringUtils.isEmpty(param.getPlatformId())){
                throw new ServiceException("platformId不能为空");
            }
            order = driverOrderService.getDriverOrderByPlatformId(param.getPlatformId());
            param.setOrderId(order.getOrderId());

            //删除原有评价信息
            orderEvaluateMapper.deleteByOrderId(order.getOrderId());
        }
        if(null == order){
            throw new ServiceException("订单查询失败");
        }
//        Integer count = orderEvaluateMapper.isEvaluated(param.getOrderId());
//        if(count > 0){
//            throw new ServiceException("不可重复评价/投诉");
//        }


        OrderEvaluateDriver driver = new OrderEvaluateDriver();
        driver.setType(param.getType());
        driver.setEvaluateReason(param.getEvaluateReason());
        driver.setEvaluateDetail(param.getEvaluateDetail());
        driver.setPassengerId(userId);
        driver.setPassengerTel(param.getPassengerTel());
        driver.setStatus("0");
        driver.setOrderId(param.getOrderId());
        if(param.getType().equals("0")){//差评
            driver.setScore(0);
        }else if(param.getType().equals("1")){//中评
            driver.setScore(5);
        }else if(param.getType().equals("2")){//好评
            driver.setScore(10);
        }else if(param.getType().equals("3")){//投诉
            driver.setScore(-5);
        }
        driver.setCompanyId(order.getCompanyId());
        driver.setDriverName(order.getEmployeeName());
        driver.setEmployeeId(order.getEmployeeId());
        driver.setCarNo(order.getCarNo());
        driver.setEvaluateTime(new Date());
        driver.setCreateTime(DateUtils.getNowDate());
        return orderEvaluateMapper.addOrderevaluate(driver);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GetOrderEvaluateHandleVO getHandle(GetOrderEvaluateHandleParam param) {
        GetOrderEvaluateHandleVO handle = orderEvaluateMapper.getHandle(param.getOrderEvaluateId());
        if(handle == null){
            return new GetOrderEvaluateHandleVO();
        }
        return handle;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int editOrderEvaluate(EditOrderEvaluateHandleParam param) {
        if(param.getIsSatisfied()){
            param.setStatus("3");
        }if(!param.getIsSatisfied()){
            param.setStatus("2");
        }
        return orderEvaluateMapper.editOrderEvaluate(param);
    }
}
